package com.crm.service.permission;

import com.crm.dao.permission.RoleDAO;
import com.crm.dao.permission.UserInfoDAO;
import com.crm.exception.ServiceException;
import com.crm.pojo.CrmDept;
import com.crm.pojo.Inandoutlog;
import com.crm.pojo.Userinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class UserInfoService {

    @Autowired
    private UserInfoDAO userInfoDAO;
    
    @Autowired
    private RoleDAO roleDAO;

    public String queryRoleNameUsers(Long uId){
//    	System.out.println("发送到");
    	return roleDAO.queryRoleNameUser(uId);
    	
    }
    
    /**
     * 分页查询用户
     *
     * @param curpage
     * @param pagesize
     * @return
     */
    public List<Userinfo> queryAllUser(int curpage, int pagesize) {
        PageRequest page = new PageRequest(curpage - 1, pagesize);
        return userInfoDAO.queryAllPage(page);
    }


	/**
	 * 分页查询总记录数
	 * 
	 * @return
	 */
	public Long queryPageCount() {
		return userInfoDAO.queryUserCount();
	}

	/**
	 * 根据用户名模糊查询查询总记录数
	 * 
	 * @return
	 */
	public Long queryNamePageCount(String uname) {
		return userInfoDAO.queryNameUserCount("%" + uname + "%");
	}

	public Userinfo queryOneName(String uname) {
		return userInfoDAO.queryNameUser(uname);
	}

	public List<String> queryNameAllUpdate(){
		return userInfoDAO.queryNameUpdate();
	}
	


	/**
	 * 根据用户名模糊查询
	 * 
	 * @param uname
	 * @return
	 */
	public List<Userinfo> queryNameUsernifo(String uname, int curpage, int pagesize) {
		PageRequest page = new PageRequest(curpage - 1, pagesize);
		return userInfoDAO.queryNameUser("%" + uname + "%", page);
	}

   
	/**
	 * 添加用户
	 * 
	 * @param uname
	 * @param upassword
	 * @param urealName
	 * @param phone
	 */
	public void addUserInfo(String uname, String upassword, String urealName, String phone, String userAddress,
			CrmDept crmDeptno) throws ServiceException {
		try {
			Userinfo userinfo = new Userinfo();
			userinfo.setUname(uname);
			userinfo.setUpassword(upassword);
			userinfo.setUrealName(urealName);
			userinfo.setPhone(phone);
			userinfo.setUserAddress(userAddress);
			userinfo.setUcreateTime(new Date());
			userinfo.setCrmDept(crmDeptno);
			userinfo.setUstatus("是");
			userInfoDAO.save(userinfo);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}

 

    /**
     * 删除一个用户
     *
     * @param uId
     */
    public void deleteUserInfo(Long uId) {
        Userinfo ui = userInfoDAO.findOne(uId);
        ui.setUstatus("否");
        userInfoDAO.save(ui);
    }

    /**
     * 重置用户密码为123456
     *
     * @param uId
     */
    public void resetPassword(Long uId) {
        Userinfo ui = userInfoDAO.findOne(uId);
        ui.setUpassword("123456");
        userInfoDAO.save(ui);
    }



    /**
     * 根据用户ID查找用户信息
     *
     * @param uId
     * @return
     */
    public Userinfo getOne(Long uId) {
        return userInfoDAO.findOne(uId);
    }

    /**
     * 修改用户信息
     *
     * @param uId
     * @param uname
     * @param upassword
     * @param urealName
     * @param phone
     * @param userAddress
     * @param CrmDeptno
     * @param ustatus
     */
    public void updateUser(Long uId, String uname, String urealName, String phone, String userAddress,
                           CrmDept CrmDeptno) {
        Userinfo ui = userInfoDAO.findOne(uId);
        ui.setUname(uname);
        ui.setUrealName(urealName);
        ui.setPhone(phone);
        ui.setUserAddress(userAddress);
        ui.setCrmDept(CrmDeptno);
        userInfoDAO.save(ui);

    }

    /**
     * 获取整个员工数据列表
     *
     * @return
     */
    public List<Userinfo> getUserInfoList() {
        List<Userinfo> list = new ArrayList<>();
        userInfoDAO.findAll().forEach(e -> {
            list.add(e);
        });
        return list;
    }
	//模糊查询经手人通过经手人昵称或名称
	public List<Inandoutlog> findAllUsersBymh(String q){
		List<Inandoutlog> list = new ArrayList<>();
		List<Userinfo> ulist = userInfoDAO.findAllUsersBymh("%"+q+"%");
		for (Userinfo u:ulist) {
			Inandoutlog inandoutlog = new Inandoutlog();
			inandoutlog.setHandmanId(u.getUId());
			inandoutlog.setHandman(u.getUrealName());
			inandoutlog.setNickname(u.getUname());
			list.add(inandoutlog);
		}
		return list;
	}
	//查询单个用户信息
	public Userinfo findOne(Long uid){
    	return userInfoDAO.findOne(uid);
	}


	
}
